package com.ruiben.mall.impl;

import com.ruiben.mall.ICommentService;
import com.ruiben.mall.mapper.ShopProductCommentMapper;
import com.ruiben.mall.mapper.UserCartMapper;
import com.ruiben.mall.mapper.UserOrderMapper;
import com.ruiben.mall.mapper.UserOrderProductMapper;
import com.ruiben.mall.pojo.ShopProductComment;
import com.ruiben.mall.pojo.UserOrder;
import com.ruiben.mall.pojo.UserOrderProduct;
import com.ruiben.mall.utils.ImageUtils;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruiben.mall.config.Config.BASE_URL;

@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private ShopProductCommentMapper commentMapper;

    @Autowired
    private UserOrderMapper userOrderMapper;

    @Autowired
    private UserOrderProductMapper userOrderProductMapper;

    @Autowired
    private Sid sid;


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean addComment(String userOrderProductId,ShopProductComment comment,String[] imgs) {
        comment.setId(sid.nextShort());
        comment.setCommenttime(new Date());
        if (!StringUtils.isBlank(comment.getCommentpic0())){
            comment.setCommentpic0(ImageUtils.replaceImg(comment.getCommentpic0()));
        }

        if (!StringUtils.isBlank(comment.getCommentpic1())){
            comment.setCommentpic1(ImageUtils.replaceImg(comment.getCommentpic1()));
        }

        if (!StringUtils.isBlank(comment.getCommentpic2())){
            comment.setCommentpic2(ImageUtils.replaceImg(comment.getCommentpic2()));
        }

        if (imgs !=null&&imgs.length>0){
            if (imgs.length == 1){
                comment.setCommentpic0(imgs[0]);
            } else if (imgs.length==2){
                comment.setCommentpic0(imgs[0]);
                comment.setCommentpic1(imgs[1]);
            } else if (imgs.length==3){
                comment.setCommentpic0(imgs[0]);
                comment.setCommentpic1(imgs[1]);
                comment.setCommentpic2(imgs[2]);
            }

        }
        commentMapper.insert(comment);


        UserOrderProduct userOrderProduct = userOrderProductMapper.selectByPrimaryKey(userOrderProductId);
        userOrderProduct.setIscomment((short)1);
        userOrderProductMapper.updateByPrimaryKey(userOrderProduct);

        //获取该订单下的所有商品，判断是否有未完成的评论，如果没有，修改状态
        UserOrder orderDetailById = userOrderMapper.getOrderDetailById(comment.getOrderid(), 4);
        List<UserOrderProduct> orderProducts = orderDetailById.getOrderProduct();

        List<UserOrderProduct> commentGoods = new ArrayList<>();
        for (UserOrderProduct product:orderProducts){
            if (product.getIscomment() == null || product.getIscomment() == 0){
                commentGoods.add(product);
            }
        }
        if (commentGoods == null || commentGoods.size() == 0) {
            UserOrder userOrder = new UserOrder();
            userOrder.setId(comment.getOrderid());
            userOrder.setOrderstatus((short)5);
            userOrderMapper.updateByPrimaryKeySelective(userOrder);
        }



        return true;
    }
}
